package cn.itguy.mobileguard.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import cn.itguy.mobileguard.ActivityJumper;
import cn.itguy.mobileguard.CommonUtils;
import cn.itguy.mobileguard.IMGPreferences;
import cn.itguy.mobileguard.R;
import cn.itguy.mobileguard.fragment.AlarmFragment;
import cn.itguy.mobileguard.fragment.MoreFragment;
import cn.itguy.mobileguard.service.IGuardService;
import cn.itguy.mobileguard.widget.TabPagerAdapter;

public class MainActivity extends BaseActivity implements
		OnCheckedChangeListener, OnPageChangeListener {

	private static final int TAB_INDEX_ALARM = 0;
	private static final int TAB_INDEX_MORE = 1;

	private RadioGroup rg_tab;
	private TabPagerAdapter pagerAdapter;

	private ViewPager mPager;

	private int mCurrentPosition = -1;
	private int mNextPosition = -1;

	public interface ViewPagerVisibilityListener {
		public void onVisibilityChanged(boolean visible);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		PackageManager pm = getPackageManager();
		try {
			PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
			int vCode = packageInfo.versionCode;
			IMGPreferences preferences = IMGPreferences.getInstance(this);
			int currVCode = preferences.getCurrentVersionCode();
			if (vCode != currVCode) {
				preferences.setCurrentVersionCode(vCode);
				ActivityJumper.toHelpActivity(this);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		
		setContentView(R.layout.main);

		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new AlarmFragment());
		fragments.add(new MoreFragment());
		pagerAdapter = new TabPagerAdapter(getSupportFragmentManager(),
				fragments);

		mPager = (ViewPager) findViewById(R.id.contentPager);
		mPager.setAdapter(pagerAdapter);
		mPager.setOnPageChangeListener(this);

		rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
		rg_tab.setOnCheckedChangeListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();

		mCurrentPosition = mPager.getCurrentItem();
		sendFragmentVisibilityChange(mCurrentPosition, true);
	}

	private void sendFragmentVisibilityChange(int position, boolean visibility) {
		final Fragment fragment = pagerAdapter.getItem(position);
		if (fragment instanceof ViewPagerVisibilityListener) {
			((ViewPagerVisibilityListener) fragment)
					.onVisibilityChanged(visibility);
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		int item;
		switch (checkedId) {
		case R.id.rb_alarm:
			item = TAB_INDEX_ALARM;
			break;
		case R.id.rb_more:
			item = TAB_INDEX_MORE;
			break;

		default:
			item = TAB_INDEX_ALARM;
			break;
		}
		mPager.setCurrentItem(item);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
			if (mCurrentPosition >= 0) {
				sendFragmentVisibilityChange(mCurrentPosition, false);
			}
			if (mNextPosition >= 0) {
				sendFragmentVisibilityChange(mNextPosition, true);
			}
			mCurrentPosition = mNextPosition;
		}
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		int id;
		switch (arg0) {
		case TAB_INDEX_ALARM:
			id = R.id.rb_alarm;
			break;
		case TAB_INDEX_MORE:
			id = R.id.rb_more;
			break;
		default:
			id = R.id.rb_alarm;
			break;
		}
		rg_tab.check(id);

		mNextPosition = arg0;
	}

	@Override
	public void onBackPressed() {
		showQuitDialog();
	}

	/**
	 * show a dialog to let user decide whether quit or not.
	 */
	private void showQuitDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.tipText_quitApp)
				.setMessage(R.string.contentText_quitApp)
				.setPositiveButton(R.string.btnText_confirm,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								boolean running = CommonUtils.isServiceRunning(MainActivity.this,
										IGuardService.class.getName());
								if (running) {
									Toast.makeText(MainActivity.this, R.string.tipText_alarm_on, Toast.LENGTH_SHORT).show();
									return;
								}
								finish();
							}
						})
				.setNeutralButton(R.string.btnText_hide, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						moveTaskToBack(true);
					}
				})
				.setNegativeButton(R.string.btnText_cancel, null)
				.show();
	}

}